package alp.starcode.framework.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @author 芊宝宝
 * @create 2021/2/14
 */
@Configuration
public class CorsConfig {
    public CorsConfig() {

    }

    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
        //config.addAllowedOrigin("http://127.0.0.1:8080");
        config.addAllowedOrigin("*");
        //设置允许发送cookie信息
        config.setAllowCredentials(true);
        //设置允许的方法get post delete
        config.addAllowedMethod("*");
        //设置允许的头部信息
        config.addAllowedHeader("*");
        config.addAllowedMethod(HttpMethod.OPTIONS);
        //为url添加映射信息
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);

        //返回定义好的sourse
        return new CorsFilter(source);
    }
}
